package demo6;

import java.lang.reflect.Constructor;

/**
 * 反射机制可以破除单例模式(需谨慎)
 */
public class Demo3 {
    public static void main(String[] args) throws Exception {
        /*Class cls = Class.forName("demo6.Student");

        //Class对象的newInstance方法只能调用公开的无参构造
        //Object o1 = cls.newInstance();
        //System.out.println("o1="+o1);

        //通过反射机制调用私有的构造
        Constructor c = cls.getDeclaredConstructor();
        c.setAccessible(true);//强行打开访问权限
        Object o1 = c.newInstance();
        Object o2 = c.newInstance();

        System.out.println("o1="+o1);
        System.out.println("o2="+o2);*/

        Class cls = Class.forName("demo6.Student");
        Constructor c= cls.getDeclaredConstructor();
        c.setAccessible(true);
        Object o1 = c.newInstance();
        Object o2 = c.newInstance();
    }
}
